package com.puyuan.web;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class BaseServlet extends HttpServlet {

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //解决乱码
        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=utf-8");
        //获取要访问的方法名
        String methodName = req.getParameter("method");
        //使用反射来执行这个方法
        Class<? extends BaseServlet> Class = this.getClass();
        //获取方法的反射对象
        Method method = null;
        try {
            method = Class.getDeclaredMethod(methodName, HttpServletRequest.class, HttpServletResponse.class);
            method.invoke(Class.newInstance(),req,resp);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
